package com.why.controller;

import com.why.dto.ArticlePageDTO;
import com.why.pojo.Article;
import com.why.result.PageResult;
import com.why.result.Result;
import com.why.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/article")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    //文章列表--未分页
    @GetMapping("/list")
    public Result<List<Article>> list(){
        log.info("***文章列表--未分页***");
        List<Article> list = articleService.getAll();
        return Result.success(list);
    }

    //新增文章
    @PostMapping("/add")
    public Result<Article> add(@RequestBody @Validated Article article){
        log.info("***新增文章***");
        articleService.add(article);
        Integer id = article.getId();
        Article byId = articleService.getById(id);
        return Result.success(byId);
    }

    //根据id查询文章
    @GetMapping("/getById")
    public Result<Article> getById(@RequestParam Integer id){
        log.info("***根据id查询文章***");
        Article byId = articleService.getById(id);
        return Result.success(byId);
    }

    //分页条件查询
    @PostMapping("/page")
    public Result<PageResult<Article>> page(@RequestBody ArticlePageDTO articlePageDTO){
        log.info("***分页条件查询***");
        PageResult<Article> result = articleService.pageQuery(articlePageDTO);
        return Result.success(result);
    }

    //修改文章信息
    @PutMapping("/update")
    public Result<Article> update(@RequestBody Article article){
        log.info("***修改文章信息***");
        articleService.updateArticle(article);
        Article byId = articleService.getById(article.getId());
        return Result.success(byId);
    }

    //批量删除文章
//    @DeleteMapping("/delete")
//    public Result<List<Integer>> delete(@RequestParam List<Integer> ids){
//        log.info("***批量删除文章***");
//        articleService.delete(ids);
//        return Result.success(ids);
//    }

    //根据id删除文章
    @DeleteMapping("/delete")
    public Result<Integer> delete(@RequestParam Integer id){
        log.info("***批量删除文章***");
        articleService.removeById(id);
        return Result.success(id);
    }
}
